#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2020 OpenWrt.org

START=99

start_instance() {
    local cfg="$1"

    config_get type "$cfg" TYPE

    case "$type" in
      edge)
        config_get_bool enabled "$cfg" 'enabled' '0'
        [ "$enabled" = "0" ] && return 1
        config_get tunname "$cfg" 'tunname'
        config_get mode "$cfg" 'mode'
        config_get ipaddr "$cfg" 'ipaddr'
        config_get prefix "$cfg" 'prefix'
        config_get mtu "$cfg" 'mtu'
        config_get supernode "$cfg" 'supernode'
        config_get port "$cfg" 'port'
        config_get second_supernode "$cfg" 'second_supernode'
        config_get second_port "$cfg" 'second_port'
        config_get community "$cfg" 'community'
        config_get key "$cfg" 'key'
        config_get_bool route "$cfg" 'route' '0'
        address="$ipaddr"
        supernode_bak=""
        [ "$second_supernode" -a "$second_port" ] && supernode_bak=" -l ${second_supernode}:${second_port}"
        [ "$route" = "1" ] && args='-r'
        [ "$mode" = 'dhcp' ] && address='0.0.0.0'
        [ "-$mtu" != "-" ] && mtu="-M $mtu"
        eval "$(ipcalc.sh "$ipaddr/$prefix")"
        netmask="$NETMASK"
        /usr/bin/edge -u 0 -g 0 -d $tunname -a ${mode}:${address} -s $netmask -c $community $([ -n "$key" ] && echo -k $key) -l ${supernode}:${port}$supernode_bak $args $mtu
        iptables -I FORWARD -i "$tunname" -j ACCEPT -m comment --comment 'n2n edge eth'
        iptables -I FORWARD -o "$tunname" -j ACCEPT -m comment --comment 'n2n edge eth'
        iptables -t nat -I POSTROUTING -o "$tunname" -j MASQUERADE -m comment --comment 'n2n edge net'
      ;;
      supernode)
        config_get_bool enabled "$cfg" 'enabled' '0'
        [ "$enabled" = "0" ] && return 1
        config_get port "$cfg" 'port'
        /usr/bin/supernode -l $port &
        iptables -I INPUT -p udp --dport $port -j ACCEPT -m comment --comment 'n2n supernode port'
      ;;
      route)
        config_get_bool enabled "$cfg" 'enabled' '0'
        [ "$enabled" = "0" ] && return 1
        config_get ip "$cfg" 'ip'
        config_get mask "$cfg" 'mask'
        config_get gw "$cfg" 'gw'
        route add -net $ip/$mask gw $gw
      ;;
    esac
}

stop_instance() {
    local cfg="$1"

    config_get type "$cfg" TYPE

    case "$type" in
      edge)
        config_get tunname "$cfg" 'tunname'
        iptables -D FORWARD -i "$tunname" -j ACCEPT -m comment --comment 'n2n edge eth' 2>/dev/null
        iptables -D FORWARD -o "$tunname" -j ACCEPT -m comment --comment 'n2n edge eth' 2>/dev/null
        iptables -t nat -D POSTROUTING -o "$tunname" -j MASQUERADE -m comment --comment 'n2n edge net' 2>/dev/null
        killall -9 edge
      ;;
      supernode)
        config_get port "$cfg" 'port'
        iptables -D INPUT -p udp --dport $port -j ACCEPT -m comment --comment 'n2n supernode port' 2>/dev/null
        ps | grep supernode | grep -v grep 2>&1 >/dev/null && killall -9 supernode
      ;;
    esac
}

start() {
    config_load 'n2n_v2'
    config_foreach start_instance 'edge'
    config_foreach start_instance 'supernode'
    sleep 2
    config_foreach start_instance 'route'
}

stop() {
    config_load 'n2n_v2'
    config_foreach stop_instance 'edge'
    config_foreach stop_instance 'supernode'
}
